<?php
namespace app\beautiful\controller\admin\appointment;
use app\BaseController;
use app\beautiful\model\appointment\PerformanceItemModel;
use app\beautiful\model\appointment\PerformanceModel;
use app\beautiful\model\StaffModel;
use app\common\logic\DataLogic;

class EmployeePerformance extends BaseController{
    public function index()
    {
        $staff= $this->request->param('staff');
        $time_type=$this->request->param('time_type/d',1);
        if(!in_array($time_type,[1,2,3])) return $this->error('参数错误');
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['status','=',8],
        ];

        if(!empty($staff)){
            $staff_ids=StaffModel::where([
                    ['shop_id','=',$this->request->shopId],
                    ['is_delete','=',0],
                    ['status','=',1],
                    ['name|mobile','=',$staff]
                ])->column('staff_id') ?? [];
            $where[]=['staff_id','in',$staff_ids];
        }

        $performance_model= new PerformanceModel();
        $performance_model=$performance_model->where($where);

        switch ($time_type){
            case 1:
                $performance_model = $performance_model->whereMonth('add_time');
                break;
            case 2:
                $performance_model = $performance_model->whereMonth('add_time','last month');
                break;
            case 3:
                $performance_model = $performance_model->whereYear('add_time');
                break;
        }

        $order=['commission'=>'desc'];
        $field='staff_id,sum(commission) as commission';
        $total=$performance_model->group('staff_id')->field($field)->count() ?? 0;
        $list=$performance_model->with(['staff'])
            ->order($order)
            ->group('staff_id')
            ->field($field)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['rank']=$this->request->limit * ($this->request->page - 1) + $k + 1;
            }
        }
        $return=[
            'total'=>$total,
            'list'=>$list,
        ];

        $total_commission=$performance_model->sum('commission') ?? 0;
        $return['total_commission']=$total_commission;

        return $this->success($return);

    }

    public function getEmployeePerformanceDetail()
    {
        $staff_id=$this->request->param('staff_id/d',0);
        $month=$this->request->param('month',date('Y-m',$this->request->time()));
        $where = [
            ['shop_id','=',$this->request->shopId],
            ['staff_id','=',$staff_id],
            ['status','=',8]
        ];

        $performance_item_model=new PerformanceItemModel();
        $performance_item_model=$performance_item_model->whereMonth('add_time',$month);
        $order=['add_time'=>'desc'];
        $field='*';
        $append=['add_time_format'];
        $return = DataLogic::getDataList($performance_item_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        $performance=PerformanceItemModel::where($where)
            ->whereMonth('add_time',$month)
            ->field('count(*) as count, sum(commission) as commission')
            ->find();
        $return['total_num']=$performance->count ?? 0;
        $return['total_commission']=$performance->commission===null ? 0 : $performance->commission;

        return $this->success($return);
    }


}